<?php

namespace app\api\controller\v1\home;

use app\api\model\BannerModel;
use app\api\model\CateGoryModel;
use app\api\model\GoodsModel;
use app\middleware\ApiAuth;
use think\Request;
use think\facade\Validate;
use think\response\Json;
use think\validate\ValidateRule;
use app\api\model\OrganizationModel;
/**
 * Class C端首页控制器
 * @package app\controller
 */
class Index
{
    protected $middleware = [ApiAuth::class];

    /**
     * @desc 获取首页数据
     * @return Json
     */
    public function index(Request $request)
    {
        // 获取Access_token
        $input['admin_id'] = $request->param('admin_id');//code
        // 参数校验
        $validate = Validate::rule([
            'admin_id' => ValidateRule::isRequire(),
        ]);
        if (!$validate->check($input)) {
            return jsonFail($validate->getError());
        }
        //逻辑处理
        $banner = BannerModel::GetBanner($input);//TODO 首页banner图
        $categroy = CateGoryModel::GetCategory($input);//TODO 一级分类
        $is_new = GoodsModel::GetGoodsNew($input);//TODO 是否新品
        $is_hot = GoodsModel::GetGoodsHot($input);//TODO 是否热品
        return jsonSuccess('获取成功', compact('banner', 'categroy', 'is_new', 'is_new', 'is_hot'));
    }

    /**
     * @desc 商品搜索
     * @return Json
     */
    public function search(Request $request)
    {
        // 获取Access_token
        $input['text'] = $request->param('text');//字符串
        $input['cate_id'] = $request->param('cate_id');//一级分类
        $input['admin_id'] = $request->param('admin_id');//运营商ID
        $input['limit'] = $request->param('limit', 10);//条数
        $input['page'] = $request->param('page', 1);//页
        $input['priceOrder'] = $request->param('priceOrder');//价格排序
        $input['salesOrder'] = $request->param('salesOrder');//销量排序
        $input['is_new'] = $request->param('is_new', 0);//是否新品
        $input['is_hot'] = $request->param('is_hot', 0);//是否热卖
        // 参数校验
        $validate = Validate::rule([
//            'text' => ValidateRule::isRequire(),
            'admin_id' => ValidateRule::isRequire(),
        ]);
        if (!$validate->check($input)) {
            return jsonFail($validate->getError());
        }
        // 逻辑处理
        return GoodsModel::search($input);
    }

    /**
     * @desc 商品详情
     * @return Json
     */
    public function detail(Request $request)
    {
        $input['admin_id'] = $request->param('admin_id');//运营商ID
        $input['goods_id'] = $request->param('goods_id');//商品ID
        // 参数校验
        $validate = Validate::rule([
            'goods_id' => ValidateRule::isRequire(),
            'admin_id' => ValidateRule::isRequire(),
        ]);
        if (!$validate->check($input)) {
            return jsonFail($validate->getError());
        }
        //逻辑处理
        return GoodsModel::detail($input);
    }
    /**
     * @desc 搜索小区站点
     * @return Json
     */
    public function SearchSite(Request $request)
    {
        $input['admin_id'] = $request->param('admin_id');//运营商ID
        $input['province'] = $request->param('province');//省
        $input['city'] = $request->param('city');//市
        $input['area'] = $request->param('area');//省
        $input['text'] = $request->param('text');//站点名字
        // 参数校验
        $validate = Validate::rule([
            'admin_id' => ValidateRule::isRequire(),
        ]);
        if (!$validate->check($input)) {
            return jsonFail($validate->getError());
        }
        //逻辑处理
        return OrganizationModel::SearchSite($input);
    }
}
